package com.alexgaoyh.MutiModule.Dubbo.RWSeperate.consumer.web.sysman.index;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.MutiModule.common.utils.CookieUtilss;
import com.MutiModule.common.utils.DubboRWSepConWebPropUtilss;
import com.MutiModule.common.utils.JSONUtilss;
import com.MutiModule.common.utils.StringUtilss;
import com.MutiModule.common.vo.TreeNode;
import com.alexgaoyh.MutiModule.Dubbo.RWSeperate.consumer.web.demo.util.TreeNodeUtilss;

@Controller
@RequestMapping(value="")
public class IndexController {

	/**
	 * 首页
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/")
	public ModelAndView index(ModelAndView model, HttpServletRequest request) {
		model.setViewName("index");
		return model;
	}
	
	/**
	 * 登陆校验方法
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/toLogin")
	public ModelAndView toLogin(ModelAndView model, HttpServletRequest request, HttpServletResponse response) {
		String userName = request.getParameter("userName");
		String password = request.getParameter("password");
		
		if(StringUtilss.isNotEmpty(userName) && StringUtilss.isNotEmpty(password)) {
			
			if("admin@admin.com".equals(userName) && "admin@admin.com".equals(password)) {
				//登陆成功
				CookieUtilss.add(response, DubboRWSepConWebPropUtilss.getAdminCookie(), "admin@admin.com", 30*30*60, "/");
				
				model.setViewName("redirect:/manager/index");
			} else {
				//登陆失败
				model.setViewName("redirect:/");
			}
		} else {
			//缺失参数
			model.setViewName("redirect:/error/10006");
		}
		
		return model;
	}
	
	@RequestMapping(value = "/manager/index")
	public ModelAndView manager(ModelAndView model, HttpServletRequest request) {
		
		List<TreeNode> treeList = TreeNodeUtilss.generaterTreeNodeList2();
		
		model.addObject("treeList", treeList);
		
		model.setViewName("manager/index");
		return model;
	}
	
	@RequestMapping(value = "/logOut")
	public ModelAndView logOut(ModelAndView model, HttpServletRequest request, HttpServletResponse response) {
		
		CookieUtilss.remove(request, response, DubboRWSepConWebPropUtilss.getAdminCookie(), "/");
		
		model.setViewName("redirect:/");
		return model;
	}
}
